<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>如何通过事件向父组件传递参数，打破单项数据流的限制。</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/3.0.2/vue.global.js"></script>
</head>

<body>
    <div id="app"></div>
</body>
<script>
    const app = Vue.createApp({
        data() {
            return {
                counter: 0
            }
        },
        //接收子组件传递过来的参数
        methods: {
            handleAddCounter(result) {
                this.counter = result
            }
        },        
        template: `
            <h1>唯爱薇o朝圣之路</h1>
            <Counter :counter="counter" @add="handleAddCounter"/>
            <hr\>
            <Counter :counter="counter"/>
            <hr\>
            <Counter :counter="counter"/>
            <hr\>      
        `
    })
    app.component('Counter', {
        props: ['counter'],
        emits: {
            //校验传递数据逻辑写法
            add: (value) => {
                return value < 20 ? true : false
            }
        },
        methods: {
            //子组件调用的并不是父组件中的方法，而是子组件中的方法。
            //与此同时这种方法无法保证组件的独立性，这个是需要注意的
            //子组件需要向父组件传值
            handleClick(){
                this.$emit('add', this.counter + 2)
            }
        },
        template: `
            {{counter}}
            <br/>
            <button @click="handleClick">增加数量</button>
        `
    })

    const vm = app.mount("#app")
</script>

</html>